package com.orifine.orm.type;

import com.orifine.orm.Information;
import com.orifine.orm.Type;
import com.orifine.orm.information.CharRandomInfo;
import com.orifine.orm.model.ColumnModel;

public class StringType extends Type {
	private String value;
	
	public StringType(){
		
	}
	
	public StringType(String value){
		this.value = value;
	}
	
	private VarChar varChar = new VarChar();
	private Text text = new Text();
	private MediumText mediumText = new MediumText();
	private LongText longText = new LongText();
	
	@Override
	public String getScriptType(ColumnModel column) {
		if(column.getLength().getValue() < 0){
			throw new RuntimeException(column.getName()+"的长度不能小于0");
		} else if(column.getLength().getValue() <= VarChar.LENGTH_MAX){
			return varChar.getScriptType(column);
		} else if(column.getLength().getValue() <= Text.MAX_LENGTH) {
			return text.getScriptType(column);
		} else if(column.getLength().getValue() <= MediumText.MAX_LENGTH) {
			return mediumText.getScriptType(column);
		} else if(column.getLength().getValue() <= LongText.MAX_LENGTH){
			return longText.getScriptType(column);
		} else {
			throw new RuntimeException(column.getField()+"的Length过大");
		}
	}

	@Override
	public String getScriptValue(ColumnModel column) {
		if(value == null) return "NULL";
		return filterQuote(value);
	}

	@Override
	public Information getDefaultInfo(ColumnModel column) {
		return new CharRandomInfo(column.getLength().getValue());
	}
}
